﻿create procedure [Assert].[HasRowCount] ( @p_SqlStatement nvarchar(max), @p_ExpectedRowCount int )
as
begin

    declare @sql nvarchar(max) = N'select @RowCountOutput = Count(1) from OpenRowSet(''SQLOLEDB'',''Server=' + @@SERVERNAME + ';Trusted_Connection=Yes;'',''' + @p_SqlStatement + ''')';
    declare @RowCount int;

    exec sp_executesql @sql, N'@RowCountOutput int output', @RowCountOutput = @RowCount output;

    if @RowCount <> @p_ExpectedRowCount
    begin
      declare @FailureMessage varchar(max) = 'Assert.HasRowCount failed : Read ' + Convert( varchar, @RowCount ) + ' rows, expected ' + Convert( varchar, @p_ExpectedRowCount ) + '. SQL Statement : ' + @p_SqlStatement;
      exec Assert.Fail @FailureMessage
    end

end